package main.graph.tests;

import java.util.ArrayList;
import java.util.HashMap;

import y.base.Edge;
import y.base.EdgeCursor;
import y.base.EdgeList;
import y.base.EdgeMap;
import y.base.Node;
import y.base.YCursor;
import y.base.YList;

import main.graph.GraphAlgo;
import main.graph.ValuedGraph;


public class GraphAlgoTests{

	public static void main(String[] args) {
		
		ValuedGraph g=new ValuedGraph();
		
		ArrayList<Node> fixed = new ArrayList<Node>();
		
		Node n0=g.createNode();g.putNodeValue(n0,"n0");
		Node n1=g.createNode();g.putNodeValue(n1,"n1");
		Node n2=g.createNode();g.putNodeValue(n2,"n2");
		Node n3=g.createNode();g.putNodeValue(n3,"n3");
		Node n4=g.createNode();g.putNodeValue(n4,"n4");
		Node n5=g.createNode();g.putNodeValue(n5,"n5");
		Node n6=g.createNode();g.putNodeValue(n6,"n6");
		
		fixed.add(n0);
		fixed.add(n1);
		fixed.add(n2);
		fixed.add(n3);
		fixed.add(n4);
		fixed.add(n6);
		
		EdgeMap weights= g.createEdgeMap();
		
		Edge e0=g.createEdge(n0,n1);
		g.putEdgeValue(e0,"a");weights.setDouble(e0,1);
		Edge e1=g.createEdge(n0,n1);
		g.putEdgeValue(e1,"b");weights.setDouble(e1,1);
		Edge e2=g.createEdge(n1,n2);
		g.putEdgeValue(e2,"c");weights.setDouble(e2,1);
		Edge e3=g.createEdge(n1,n3);
		g.putEdgeValue(e3,"f");weights.setDouble(e3,1);
		Edge e4=g.createEdge(n2,n3);
		g.putEdgeValue(e4,"g");weights.setDouble(e4,1);
		Edge e5=g.createEdge(n0,n6);
		g.putEdgeValue(e5,"d");weights.setDouble(e5,1);
		Edge e6=g.createEdge(n0,n6);
		g.putEdgeValue(e6,"e");weights.setDouble(e6,1);
		Edge e7=g.createEdge(n0,n4);
		g.putEdgeValue(e7,"j");weights.setDouble(e7,1);
		Edge e8=g.createEdge(n4,n5);
		g.putEdgeValue(e8,"i");weights.setDouble(e8,1);
		Edge e9=g.createEdge(n5,n6);
		g.putEdgeValue(e9,"h");weights.setDouble(e9,1);
		Edge e10=g.createEdge(n0,n2);
		g.putEdgeValue(e10,"k");weights.setDouble(e10,1);
		
		
		
		//System.out.println("All MSTs:");
		//ArrayList<EdgeList> msts=GraphAlgo.allMSTs(g, weights);
		
		System.out.println("All Steiners:");
		ArrayList<EdgeList> steiners = GraphAlgo.allSteinerTreesIntegrated(g, weights, fixed);
		
		for(int j = 0; j < steiners.size(); j++){
			System.out.println("The "+ j +"th Steiner:");
			
			EdgeList amst = steiners.get(j);
			
			for(EdgeCursor amstEc = amst.edges(); amstEc.ok(); amstEc.next()){
				
				Edge e = amstEc.edge();
			
				System.out.println(e + " [" + e.index() + ":" + g.getEdgeValue(e) + "]");
			}
		}
		
		
		/*
		System.out.println("All Shortest Paths:");
		NodeMap allPaths=GraphAlgo.allShortestPaths(g,n0,weights);
		System.out.println("To display...");
		NodeIterator iterator=g.nodes();
		while(iterator.hasNext()){
			Node n=iterator.nextNode();
			ArrayList paths=(ArrayList)allPaths.get(n);
			System.out.println("The set of shortest paths to a node:");
			for(int i=0;i<paths.size();i++){
				Path path=(Path)paths.get(i);
				System.out.println(path);
			}
		}
		
		System.out.println("All Steiner Trees:");
		ArrayList fixed=new ArrayList();
		fixed.add(n1);fixed.add(n2);fixed.add(n6);//fixed.add(n0);
		//fixed.add(n1);fixed.add(n4);
		ArrayList steiners=GraphAlgo.allSteinerTrees(g,weights,fixed);
		System.out.println("The number of steiner trees:"+steiners.size());
		for(int i=0;i<steiners.size();i++){
			Graph steiner=(Graph)steiners.get(i);
			System.out.println("The "+i+"th Steiner tree:");
			EdgeIterator it=steiner.edges();
			while(it.hasNext()){
				Edge e=it.nextEdge();
				Node source=steiner.getSource(e);
				Node target=steiner.getTarget(e);
				System.out.print("["+steiner.getNodeValue(source)+"--");
				System.out.print(steiner.getEdgeValue(e)+"--");
				System.out.println(steiner.getNodeValue(target)+"]");
			}
			System.out.println();
		}
		
		g.removeEdge(e8);
		g.removeNode(n0);
		NodeMap compNum=g.createNodeMap();
		GraphAlgo.components(g,compNum);
		NodeIterator nIt=g.nodes();
		while(nIt.hasNext()){
			Node node=nIt.nextNode();
			System.out.println("Node "+g.getNodeValue(node)+" has" +				" compNum:"+compNum.getInt(node));
		}
		*/
	}
}
